//
//  MyVC.m
//  PersonalOC
//
//  Created by 韩琴 on 2018/8/17.
//  Copyright © 2018年 韩琴. All rights reserved.
//

#import "MyVC.h"
#import "SetTableVC.h"

@interface MyVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) BaseTableView * mainTable;//这主界面
@property (strong,nonatomic) MyVCHeaderView *headerView;//界面的头
@property (copy,nonatomic) NSArray *cellTitleArrary;//标题的数组
@property (copy,nonatomic) NSArray *cellImageArrary;//图片的数组
@end

@implementation MyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //退出登录后清除所有view
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addData) name:_outLogin object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)loadUI {
    
    [self ly_setNavBarBackgroundAlpha:0];
    [self ly_setNavBarShadowImageHidden:YES];
    self.navigationItem.title = @"";
    
    
    //设置内容相对(让内容计较navgationbar)
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.mainTable = [[BaseTableView alloc] init];
    [self.view addSubview:self.mainTable];
    [self.mainTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(0);
    }];
    self.mainTable.delegate = self;
    self.mainTable.dataSource = self;
    self.headerView = [[MyVCHeaderView alloc] initWithFrame:CGRectMake(0, 0, LY_M_SCREEN_W, LY_navgationBarAndStatusBar_Height+125)];
    DLog(@"%f",LY_navgationBarAndStatusBar_Height);
    self.mainTable.tableHeaderView = self.headerView;
    self.headerView.nicknaleLale.text = @"用户昵称";
    self.headerView.backgroundColor = LY_Theme_Color;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStyleDone target:self action:@selector(didClickRightBarbutton)];
    
    
    
    [self addData];
    
}
#pragma mark --- 界面加载数据
-(void)addData {
    if ([LYTools stringIsNil:_userModel.id]) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆/注册" style:UIBarButtonItemStyleDone target:self action:@selector(didClickleftBarButtonItem)];
        [self.headerView.avatarButton setBackgroundImage:LY_default_avavtar forState:UIControlStateNormal];
        self.headerView.nicknaleLale.text = @"用户昵称";
    } else {
        self.navigationItem.leftBarButtonItem = nil;
        if ([_userModel.headPicUrl ly_isValidUrl]) {
            [LYTools sd_setImageWithimageView:self.headerView.avatarButton withImageUrl:_userModel.headPicUrl withPlaceholderImage:LY_default_avavtar withCompleted:nil];
        } else {
            [self.headerView.avatarButton setBackgroundImage:LY_IMAGE_NAMED(_userModel.headPicUrl) forState:UIControlStateNormal];
        }
        self.headerView.nicknaleLale.text = _userModel.nickName;
    }
    
    
    self.cellTitleArrary = @[@[@"我的订单",@"我的钱包"],@[@"我的收藏",@"我的分享"],@[@"联系客服"]];
    self.cellImageArrary = @[@[@"我的订单",@"我的钱包"],@[@"我的收藏",@"我的分享"],@[@"联系客服"]];
}
#pragma mark --- 点击rightBarButtonItem
- (void)didClickRightBarbutton {
    if ([LYTools stringIsNil:_userModel.id]) {
        [self didClickleftBarButtonItem];
    } else {
        SetTableVC *set = [[UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"SetTableVC"];
        [self.navigationController pushViewController:set animated:true];
    }
    
}
#pragma mark --- 点击leftBarButtonItem
- (void)didClickleftBarButtonItem {
    LoginVC *login = [[LoginVC alloc] initWithNibName:@"LoginVC" bundle:nil];
    [MyTools changeRootVc:login and:true];
}

#pragma mark --- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.cellTitleArrary.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.cellTitleArrary[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myNomalCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"myNomalCell"];
    }
    cell.textLabel.text = self.cellTitleArrary[indexPath.section][indexPath.row];
    cell.imageView.image = LY_IMAGE_NAMED(self.cellImageArrary[indexPath.section][indexPath.row]);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if (indexPath == [NSIndexPath indexPathForRow:1 inSection:1]) {
        [MyTools UMShareWithShareUrl:@"http://baidu.com" withViewController:nil withTitle:@"分享测试" withImage:LY_default_avavtar withSource:@"大家收到啦收到"];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 12;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    headerView.backgroundColor = UIColor.groupTableViewBackgroundColor;
    return headerView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end



///个人中心的headerView
@interface MyVCHeaderView ()

@end
@implementation MyVCHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self loadUI];
    return self;
}
- (void)loadUI {
    self .avatarButton = [UIButton new];
    self.nicknaleLale = [UILabel new];
    [self addSubview:self.avatarButton];
    [self addSubview:self.nicknaleLale];
    [self.avatarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.avatarButton.mas_height);
        make.height.mas_equalTo(80);
        make.top.mas_equalTo(LY_navgationBarAndStatusBar_Height);
        make.centerX.equalTo(self.mas_centerX);
    }];
    [self.nicknaleLale mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarButton.mas_bottom).mas_offset(8);
        make.centerX.equalTo(self.mas_centerX);
        make.height.mas_equalTo(17);
        
    }];
    self.nicknaleLale.font = LY_S_FONT(16);
    self.nicknaleLale.textColor = UIColor.whiteColor;
    [self.avatarButton setBackgroundImage:LY_IMAGE_NAMED(@"默认头像") forState:UIControlStateNormal];
    [LYTools viewFillet:self.avatarButton andFloat:40];
}
@end
